
/*
 * 日期格式化函数
 * @param date 日期对象
 * @param format 日期格式，默认为 YYYY-MM-DD HH:mm:ss
 */
export const formatDate = (date: Date, format = 'YYYY-MM-DD HH:mm:ss') => {
  // 获取年月日时分秒，通过 padStart 补 0
  const year = String(date.getFullYear())
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')

  // 返回格式化后的结果
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds)
}

export const themeColor = '#585CE5'

export const commonRegex = {
  phone: /^1[0-9]{10,10}$/,
  password: /^[\S]{6,12}$/,
  code: /^[A-Za-z0-9_-]+$/,
  idCard: /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/,
  decimal: /^\d+(\.\d+)?$/,
  email: /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
  video: /\.(flv|3gp|rmvb|rm|swf|mp4|mkv|avi|mov|wmv|mpg)/i,
  audio: /\.(mp3|wma|ape|flac|aac|amr|m4a|m4r|ogg|wav)/i,
  image: /\.(jpg|jpeg|png|ico|bmp|gif|tif|tga)/i,
  zip: /\.zip/i,
  pdf: /\.pdf/i,
  word: /\.(doc|docx)/i,
  excel: /\.(xlx|xlsx)/i,
  ppt: /\.(ppt|pptx)/i,
  url: /^(http|https):\/\/.+/,
  windowsFile: /[\\\\/:*?\"<>|]/,
  ip: /^(?:(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:1[0-9][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}(?:(?:2[0-5][0-5])|(?:25[0-5])|(?:1[0-9][0-9])|(?:[1-9][0-9])|(?:[0-9]))$/,
  carNo: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-HJ-NP-Z]{1}[A-HJ-NP-Z0-9]{5}[A-HJ-NP-Z0-9挂学警港澳临]?/,
}

export const commonFileAllowsExtMap = {
  video: ['flv', '3gp', 'rmvb', 'rm', 'swf', 'mp4', 'mkv', 'avi', 'mov', 'wmv', 'mpg'],
  audio: ['mp3', 'wma', 'ape', 'flac', 'aac', 'amr', 'm4a', 'm4r', 'ogg', 'wav'],
  image: ['jpg', 'jpeg', 'png', 'ico', 'bmp', 'gif', 'tif', 'tga']
}

export function computeFileExt(url) {
  return url.substring(url.lastIndexOf("."), url.length);
}

export function computeFileName(url) {
  return url.substring(url.lastIndexOf("/") + 1, url.length);
}

export function strPaddingLeft(num, length, char = '0') {
  return (Array(length).join(char) + num).slice(-length)
}

export function uniqueArray(array, key = '') {
  if (!array || array.length == 0) {
    return array
  }
  if (key) {
    return array.reduce((prev, cur) => prev.map(a => a[key]).includes(cur[key]) ? prev : [...prev, cur], []);
  }
  return array.reduce((prev, cur) => prev.includes(cur) ? prev : [...prev, cur], []);
}

export function overflow(value, max = 100) {
  if (!value) {
    return ''
  }
  if (value.length > max) {
    value = value.substring(0, max) + '...'
  }
  return value
}

export function formatFileSize(size, pointLength) {
  let unit = ''
  const units = ['B', 'K', 'M', 'G', 'TB']
  while ((unit = units.shift()) && size > 1024) {
    size = size / 1024
  }
  return (unit === 'B' ? size : size.toFixed(pointLength === undefined ? 2 : pointLength)) + unit
}

export function convertToJson(json, type = 'object') {
  try {
    let result = json ? JSON.parse(json) : (type == 'object' ? {} : [])
    if (typeof result === 'string') {
      return convertToJson(result, type)
    }
    return result
  } catch (e) {
    return type == 'object' ? {} : []
  }
}

export function convertUrlParams(): Array<any> {
  let sceneArray = []
  let search = window.location.search
  if (search) {
    search = search.replace('?', '')
    const params = search.split('&')
    params.forEach(p => {
      const array = p.split('=')
      if (array.length == 2) {
        sceneArray.push({
          key: array[0],
          value: array[1]
        })
      }
    })
  }
  return sceneArray
}

export function dateToTimeStamp(date) {
  return new Date(Date.parse(date.replace(/-/g, "/"))).getTime()
}

export function isWXBrowser(): boolean {
  return window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'
}


const wxChannel = {
  id: 1, // 4 是微信h5 暂时没写
  name: '微信支付',
  icon: 'admin-wxpay',
  color: 'text-green',
  desc: '微信支付，不止支付'
}

const alipayChannel = {
  id: 8,
  name: '支付宝',
  icon: 'admin-alipay',
  color: 'text-blue',
  desc: '支付宝 知托付！'
}

const balanceChannel = {
  id: 16,
  name: '余额支付',
  icon: 'money-bag-fill',
  color: 'text-orange',
  desc: ''
}

export function getPayChannel(needBalance: boolean = true): Array<any> {
  const result = []
  const isWx = isWXBrowser()

  if (needBalance) {
    result.push(balanceChannel)
  }

  if (isWx) {
    result.push(wxChannel)
  } else {
    result.push(alipayChannel)
  }



  return result

}

const weeks = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']

export function getWeekOptions(): Array<any> {
  return weeks.map(p => {
    return {
      label: p,
      value: p
    }
  })
}